﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Config.Enum;
using Entity;
using HighLowTriple7;
using HighLowTriple7.Entity;
using Newtonsoft.Json;
using PaymentBLL;
using Utils;
using WebTriple7.Class;
using WebTriple7.Entity;
using WebTriple7.Service;

namespace WebTriple7.GoPlay
{
    public partial class Default : System.Web.UI.Page
    {
        public BetGameResult OldSession = new BetGameResult();
        public int AccountId
        {
            set { ViewState["AccountId"] = value; }
            get
            {
                try
                {
                    return (Int32)ViewState["AccountId"];
                }
                catch
                {
                    return -1;
                }
            }
        }
        public int TypeAccount
        {
            set { ViewState["TypeAccount"] = value; }
            get
            {
                try
                {
                    return (Int32)ViewState["TypeAccount"];
                }
                catch
                {
                    return -1;
                }
            }
        }
       
        public string UserName
        {
            set { ViewState["UserName"] = value; }
            get
            {
                try
                {
                    return (string)ViewState["UserName"];
                }
                catch
                {
                    return string.Empty;
                }
            }
        }
        public string DisplayName
        {
            set { ViewState["DisplayName"] = value; }
            get
            {
                try
                {
                    return (string)ViewState["DisplayName"];
                }
                catch
                {
                    return string.Empty;
                }
            }
        }
        public int Vcoin
        {
            set { ViewState["Vcoin"] = value; }
            get
            {
                try
                {
                    return (Int32)ViewState["Vcoin"];
                }
                catch
                {
                    return -1;
                }
            }
        }
        public uspPaymentGatewaySelectResult Info = new uspPaymentGatewaySelectResult();
        protected void Page_Load(object sender, EventArgs e)
        {
            var ctr = new CtrPaymentGateway();
            Info = ctr.SelectPaymentGetway((int)GateWay.Ebank);
            if (Info == null)
                Response.Write("Not support!");

            try
            {

                GetAccessToken();

                if (UserUtils.IsLogin)
                {
                    divGame.Visible = true;
                    AccountId = UserUtils.AccountId;
                    UserName = UserUtils.UserName;
                    TypeAccount = UserUtils.TypeAccount;
                    DisplayName = UserUtils.DisplayName;                    
                }
                else
                {
                    divGame.Visible = false;
                    return;
                }

            }
            catch (Exception ex)
            {
                divGame.Visible = false;
                ltrMess.Text = "Lỗi kết nối với máy chủ";
                return;
            }


            var r = new Random();
            if (UserUtils.IsLogin)
            {
                try
                {
                    if ((UserUtils.Triple7AccessToken ?? "") == "")
                        UserUtils.Triple7AccessToken = Triple7.Instance(AccountId, UserName, TypeAccount);
                    string accessToken = UserUtils.Triple7AccessToken;
                    if (Triple7.GetGameStatus(accessToken) == 5)
                    {

                        OldSession.Info = Triple7.GetSessionInfo(accessToken);
                        OldSession.Rate = Triple7.GetRateConfig(OldSession.Info.AuCurrent + OldSession.Info.AgCurrent,
                                                                accessToken);
                        OldSession.Success = true;
                        OldSession.Mess = "";
                    }
                    else
                    {
                        OldSession.Info = new Triple7SessionInfo()
                        {
                            Column1Value = r.Next(1, 14),
                            Column2Value = r.Next(1, 14),
                            Column3Value = r.Next(1, 14)
                        };
                        OldSession.Rate = new RateConfigEntity();
                        OldSession.Success = false;
                        OldSession.Mess = "Chưa đặt cược";
                    }
                }
                catch
                {
                    AccountSession.Logout();
                }
            }
            else
            {
                OldSession.Info = new Triple7SessionInfo()
                {
                    Column1Value = r.Next(1, 14),
                    Column2Value = r.Next(1, 14),
                    Column3Value = r.Next(1, 14)
                };
                OldSession.Rate = new RateConfigEntity();
                OldSession.Success = false;
                OldSession.Mess = "Bạn chưa đăng nhập";
            }
        }

        private void GetAccessToken()
        {
            var valueCode = Request.QueryString["code"];
            var url = string.Empty;
            if (!string.IsNullOrEmpty(valueCode))
            {

                url = string.Format(Config.Global.Settings.GET_ACCESSTOKEN_URL, Config.Global.Settings.CLIENT_ID, valueCode,
                                   Config.Global.Settings.CLIENT_SECRET);

                var s = Library.Lib.SendRequest(url, "GET", null);


                var entity = JsonConvert.DeserializeObject<GoVnAccessTokenEntity>(s);
                UserUtils.GoPlayAccessToken = entity.access_token;

                var ctr = new CtrAccount();
                string uName = entity.UserId;
                string fName = entity.User;
                var info = ctr.SelectAccountInfoByName(uName);
                int? id = 0;
                if (info != null)
                {
                    if (info.Status > 0)
                    {
                        url = string.Format(Config.Global.Settings.GET_PROFILE_URL, UserUtils.GoPlayAccessToken);
                        var s1 = Library.Lib.SendRequest(url, "GET", null);                        
                        var fullpf = JsonConvert.DeserializeObject<GoVnAPIDataEntity>(s1);                        
                        AccountSession.Login(info.Id, uName, fName, (int)LoginType.GoVn, fullpf._data.publicName);
                        var gateWay = new GateWayPayment(GateWay.Ebank);
                        Vcoin = gateWay.GetBlance().Data;
                    }
                    else
                    {
                        ltrMess.Text = "Tài khoản bị khóa";
                        divGame.Visible = false;
                    }
                }
                else
                {
                    ctr.InsertAccount(uName, fName, (int)LoginType.GoVn, fName, "", 1, ref id);
                    if (!(id > 0))
                    {
                        ltrMess.Text = "Lỗi tạo tài khoản";
                        divGame.Visible = false;

                    }
                    else
                    {
                        AccountSession.Login(id ?? 0, uName, fName, (int)LoginType.GoVn);

                    }
                }
            }
            else
            {
                Response.Redirect(string.Format(Config.Global.Settings.GET_CODE_URL, Config.Global.Settings.CLIENT_ID));
            }
        }
    }
}